package com.cdzs.twocamera.util;

import com.cdzs.twocamera.constant.Constant;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;

/**
 * Created by M_genius on 2018/7/20.\
 * 描述:
 *  组装tcp连接需要的一切参数
 */

public class TcpAssembleUtil {
    /**
     * 组装请求的 Key 确保请求唯一性
     *
     * @return
     */
    public static byte[] assembleRequestId() {
        byte[] data = new byte[0];
        byte[] buf;

        for (int i = 0; i < 5; i++) {
            buf = NumberUtil.getByteArray(ByteBuffer.wrap(NumberUtil.getByteArray(Constant.REQUEST_ID)).order(ByteOrder.LITTLE_ENDIAN).getInt());
            byte[] temp = new byte[data.length + 4];
            System.arraycopy(data, 0, temp, 0, data.length);
            System.arraycopy(buf, 0, temp, data.length, 4);
            data = temp;
        }
        return data;
    }

    /**
     * 组装请求的 Key 确保请求唯一性
     *
     * @return
     */
    public static byte[] assembleRequestKey() {
        byte[] data = new byte[0];
        byte[] buf;

        for (int i = 0; i < 16; i++) {
            buf = NumberUtil.getByteArray(ByteBuffer.wrap(NumberUtil.getByteArray(Constant.REQUEST_KEY)).order(ByteOrder.LITTLE_ENDIAN).getInt());
            byte[] temp = new byte[data.length + 4];
            System.arraycopy(data, 0, temp, 0, data.length);
            System.arraycopy(buf, 0, temp, data.length, 4);
            data = temp;
        }
        return data;
    }

    /**
     * 组装 800 个字节，返回字节数组
     *
     * @return
     */
    public static byte[] assembleStruct() {

        byte[] data = new byte[0];
        byte[] buf;

        for (int i = 0; i < 200; i++) {
            buf = NumberUtil.getByteArray(ByteBuffer.wrap(NumberUtil.getByteArray(Constant.REQUEST_STRUCT_UNIT)).order(ByteOrder.LITTLE_ENDIAN).getInt());
            byte[] temp = new byte[data.length + 4];
            System.arraycopy(data, 0, temp, 0, data.length);
            System.arraycopy(buf, 0, temp, data.length, 4);
            data = temp;
        }
        return data;
    }
}
